/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.MainEventDao;
import com.rbh.risk.entity.Category;
import com.rbh.risk.entity.MainEvent;
import com.rbh.risk.support.SessionFactorySupport;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("mainEventDao")
public class MainEventDaoImpl extends SessionFactorySupport implements MainEventDao {

    @Override
    public void insert(MainEvent mainEvent) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().save(mainEvent);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void update(MainEvent mainEvent) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().update(mainEvent);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(MainEvent mainEvent) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().delete(mainEvent);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public MainEvent getById(Integer mainEventId) throws DataAccessException {
        MainEvent result = null;
        try {
            result = (MainEvent) super.getSessionFactory().getCurrentSession().get(MainEvent.class, mainEventId.shortValue());
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<MainEvent> getALL() throws DataAccessException {
        List<MainEvent> results = null;
        try {
            Query query = super.getSessionFactory().getCurrentSession().createQuery("from " + MainEvent.class.getName());
            results = query.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<MainEvent> getByCategory(Category category) throws DataAccessException {
        List<MainEvent> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(MainEvent.class);
            criteria.add(Restrictions.disjunction()
                    .add(Restrictions.eq("category", category))
                    .add(Restrictions.isNull("category")));
            results = criteria.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }
}
